package com.dhwl.hsh.driver.presenter;

import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.driver.HshApplication;
import com.dhwl.hsh.driver.R;
import com.dhwl.hsh.driver.model.impl.OrderModel;
import com.dhwl.hsh.driver.presenter.iview.IGetOrdersView;
import com.dhwl.hsh.driver.ui.activity.BaseActivity;
import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.OrderListResponse;
import com.dhwl.hsh.lib.constant.ServerConstant;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-18 18:48
 */
public class OrderPresenter extends BasePresenter<IGetOrdersView> {
    private ReservoirUtils reservoirUtils;

    public OrderPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }

    public void getOrders(final int pageNo,int orderState) {

        Subscriber<HttpResponse<List<OrderListResponse>>> subscriber = new Subscriber<HttpResponse<List<OrderListResponse>>>() {

            @Override
            public void onCompleted() {
                if (OrderPresenter.this.mCompositeSubscription != null)
                    OrderPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("merchantLogin", e.getMessage());
                ((BaseActivity) OrderPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<List<OrderListResponse>> listHttpResponse) {
                List<OrderListResponse> responseBody = listHttpResponse.getData();
                if (listHttpResponse.getStatus() != 0) {
                    HshException exception = new HshException(listHttpResponse.getStatus(), listHttpResponse.getDescribe());
                    OrderPresenter.this.getMvpView().onGetOrdersFailure(exception);
                } else {
                    OrderPresenter.this.getMvpView().onGetOrdersSuccess(responseBody);
                }
            }
        };
        this.mCompositeSubscription.add(OrderModel.getInstance().getOrders(pageNo, orderState).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                if (pageNo == ServerConstant.LIST_START_PAGE)
                    ((BaseActivity) OrderPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                if (pageNo == ServerConstant.LIST_START_PAGE)
                    ((BaseActivity) OrderPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));

    }

}
